package com.david.learning.annotation;

import java.awt.event.ActionListener;
import java.lang.reflect.Field;

import javax.swing.AbstractButton;

public class ActionListenerInstaller {

	public static void processAnnotations(Object obj) {
		try {
			Class<?> clazz = obj.getClass();
			for (Field f : clazz.getDeclaredFields()) {
				f.setAccessible(true);
				ActionListenerFor a = f.getAnnotation(ActionListenerFor.class);
				Object fObj = f.get(obj);
				if (a != null && fObj != null && fObj instanceof AbstractButton) {
					// Get the listener value declared in Annotion:
					// ActionListenerFor
					Class<? extends ActionListener> listenerClazz = a
							.listener();
					ActionListener a1 = listenerClazz.newInstance();
					AbstractButton ab = (AbstractButton) fObj;
					ab.addActionListener(a1);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
